const app = getApp()
import axios from '../../utils/axios';

Page({

  data: {
    navList: [{
      logo: '../../image/index/dog-people.png',
      name: '汪星人'
    }, {
      logo: '../../image/index/cat-people.png',
      name: '喵星人'
    }, {
      logo: '../../image/index/brand-house.png',
      name: '品牌屋'
    }, {
      logo: '../../image/index/recommend.png',
      name: '优宠推荐'
    }],
    navCoupon: {
      name: "优惠券",
      logo: '../../image/index/index-coupon.png',
      id: "coupon"
    },
    discountsList: [], // 特价商品
    commodityList: [], // 推荐好物
    couponList: [], // 优惠券
    shopLink: null, // 店铺信息
  },

  onLoad: function () {

    this.recommendGoodsType()
    this.recommendGoods()
    this.specials()
    this.getStoreInfo()
    // this.coupons()
  },

  onShow: function () {

    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }

  },

  getStoreInfo: function () {
    const self = this;
    axios('/store/storeDetail', {
      locate: "23.02067,113.75179",
      store_id: app.globalData.storeId
    }).then(res => {
      if (res.data.resultcode == 1) {

        wx.setNavigationBarTitle({
          title: res.data.result.name,
        })

        let shops = res.data.result
        self.setData({
          shopLink: shops
        })
      }
    })
  },

  // 事件委托
  getUserInfo: function (e) {

    // 当接口返回‘请登录’时，showLogin就变为true，然后调用授权
    if (e.detail.userInfo && app.globalData.showLogin) {
      const self = this
      app.signin(e.detail, function (res) {
        app.globalData.showLogin = false
        wx.navigateTo({
          url: '/pages/uifoncoupon/uifoncoupon',
        })
      })
    }

  },

  // 搜索点击事件
  checkSearch: function () {
    wx.navigateTo({
      url: '/pages/searchBox/searchBox?store_id=' + app.globalData.storeId,
    })
  },

  // 领取优惠券
  receiveCoupon: function (e) {
    console.log(e)
  },

  // 查询优惠券
  coupons: function () {
    axios('/cloud/coupons', {
      store_id: app.globalData.storeId
    }).then(res => {
      console.log(res)
      if (res.data.resultcode == 1) {
        this.setData({
          couponList: app.disposeList(res.data.result.store)
        })
      }
    })
  },

  // 查询特价商品
  specials: function () {
    axios('/cloud/specials', {
      store_id: app.globalData.storeId
    }).then(res => {
      console.log(res)
      if (res.data.resultcode == 1) {
        this.setData({
          discountsList: app.disposeList(res.data.result)
        })
      }
    })
  },

  // 查询推荐好物商品
  recommendGoods: function () {
    axios('/cloud/recommendGoods', {
      store_id: app.globalData.storeId
    }).then(res => {
      if (res.data.resultcode == 1) {
        console.log(app.disposeList(res.data.result))
        this.setData({
          commodityList: app.disposeList(res.data.result)
        })
      }
    })
  },

  // 查询导航列表
  recommendGoodsType: function () {
    axios('/cloud/recommendGoodsType', {
      store_id: app.globalData.storeId
    }).then(res => {
      console.log(res)
      if (res.data.resultcode == 1) {
        let itemJson = {
          name: "品牌屋",
          logo: '../../image/index/brand-house.png',
          id: "brand"
        }
        res.data.result.push(itemJson)
        this.setData({
          navList: res.data.result
        })
      }
    })
  },

  // 跳转商品详情
  checkDetails: function (e) {
    let goodsId = e.currentTarget.dataset.goodsid
    wx.navigateTo({
      url: '/pages/details/details?goodsId=' + goodsId
    })
  },

  // 跳转到优惠券页面
  checkCoupon: function (e) {
    if (app.globalData.userToken == '' || app.globalData.showLogin) {
      app.globalData.showLogin = true
    } else {
      wx.navigateTo({
        url: '/pages/uifoncoupon/uifoncoupon',
      })
    }

  },

  // 跳转到商品页面
  checkCommodity: function (e) {
    if (e.currentTarget.dataset.typeid == "brand") {
      app.globalData.checkNav = 'brand'
      wx.switchTab({
        url: '/pages/type/type',
      })
    } else {
      wx.navigateTo({
        url: '/pages/commodity/commodity?typeId=' + e.currentTarget.dataset.typeid + "&typeName=" + e.currentTarget.dataset.typename,
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }


})